package com.thirdgroup.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirdgroup.controller.base.BaseServlet;
import com.thirdgroup.service.OrderInfoService;
import com.thirdgroup.service.OrderPhoneService;
import com.thirdgroup.service.impl.OrderInfoServiceImpl;
import com.thirdgroup.service.impl.OrderPhoneServiceImpl;
import com.thirdgroup.utils.respond.R;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/order/*")
public class OrderServlet extends BaseServlet {
    private OrderInfoService orderInfoService=new OrderInfoServiceImpl();
    private OrderPhoneService orderPhoneService =new OrderPhoneServiceImpl();
    public void selectByManagerID(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        // 从请求体中获取 JSON 字符串
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String json = jsonBuilder.toString();

        // 使用 ObjectMapper 将 JSON 字符串转换为 Map 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> data = objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});

        // 获取ID
        String managerid = data.get("managerid");
        System.out.println("根据管理员id查看订单信息");
        System.out.println("managerid=="+managerid);
        //  {"createtime":1641052799000,"customerid":"C20220102112159",
//         "managerid":"M20220102235959","orderid":"O20220101235959",
//          "orderstatus":"活跃中", "packagecost":300.0,"packageid":"A",
//          "paymenttype":"季付","updatetime":1715775782000}

        R r = orderInfoService.selectByManagerID(managerid);
        System.out.println("根据id查找到数据如下：");
        System.out.println(r.getData());
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }
    public void updateState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        // 从请求体中获取 JSON 字符串
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String json = jsonBuilder.toString();

        // 使用 ObjectMapper 将 JSON 字符串转换为 Map 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> data = objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});

        // 获取客户ID和状态参数值
        String orderid = data.get("orderid");
        String status = data.get("status");
        System.out.println("orderid=="+orderid);
        System.out.println("status=="+status);
        // 创建当前日期对象
        Date date = new Date();

        // 创建 SimpleDateFormat 对象，指定日期时间格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 使用 SimpleDateFormat 格式化日期时间
        String formattedDate = formatter.format(date);
        R r = orderInfoService.updateState(orderid, status,formattedDate);
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }
    public void orderPhone(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        // 从请求体中获取 JSON 字符串
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String json = jsonBuilder.toString();

        // 使用 ObjectMapper 将 JSON 字符串转换为 Map 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> data = objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});

        // 获取ID
        String orderid = data.get("orderid");
        System.out.println("根据订单id查看所有电话卡");
        System.out.println("orderid=="+orderid);

        R r = orderPhoneService.orderPhone(orderid);
        System.out.println("根据订单id查找到数据如下：");
        System.out.println(r.getData());
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }

}
